﻿using System;
using System.Text;

namespace MyFastORM
{
    /// <summary>
    /// 带缩进的文本书写器，代码生成器使用该类型来生成带缩进的C#代码文本。
    /// </summary>
    internal class IndentTextWriter
    {
        /// <summary>
        /// 初始化对象
        /// </summary>
        public IndentTextWriter()
        {
        }

        /// <summary>
        /// 表示新行的字符串
        /// </summary>
        private string NewLine = System.Environment.NewLine;

        /// <summary>
        /// 内部保存数据的缓冲区
        /// </summary>
        private System.Text.StringBuilder myBuffer = new System.Text.StringBuilder();

        private string strIndentString = "   ";
        /// <summary>
        /// 缩进文本
        /// </summary>
        public string IndentString
        {
            get
            {
                return strIndentString;
            }
            set
            {
                strIndentString = value;
            }
        }

        /// <summary>
        /// 文本长度
        /// </summary>
        public int Length
        {
            get
            {
                return myBuffer.Length;
            }
        }

        /// <summary>
        /// 当前缩进层次
        /// </summary>
        private int intIndentLayer = 0;
        /// <summary>
        /// 增加缩进
        /// </summary>
        public void IncreaseIndent()
        {
            intIndentLayer++;
        }
        /// <summary>
        /// 增加缩进
        /// </summary>
        /// <param name="step">增加量</param>
        public void IncreaseIndent(int step)
        {
            intIndentLayer += step;
        }
        /// <summary>
        /// 减少缩进
        /// </summary>
        public void DecreaseIndent()
        {
            intIndentLayer--;
        }
        /// <summary>
        /// 开始输出代码组
        /// </summary>
        /// <param name="start">代码组开始符号</param>
        public void BeginGroup(string start)
        {
            WriteLine(start);
            intIndentLayer++;
        }
        /// <summary>
        /// 结束输出代码组
        /// </summary>
        /// <param name="end">代码组结束符号</param>
        public void EndGroup(string end)
        {
            intIndentLayer--;
            WriteLine(end);
        }

        /// <summary>
        /// 输出一行文本
        /// </summary>
        /// <param name="txt">文本内容</param>
        public void WriteLine(string txt)
        {
            //输出缩进字符串
            for (int iCount = 0; iCount < intIndentLayer; iCount++)
            {
                myBuffer.Append(strIndentString);
            }
            myBuffer.Append(txt);
            myBuffer.Append(NewLine);
        }
        /// <summary>
        /// 直接输出文本
        /// </summary>
        /// <param name="txt">文本</param>
        public void WriteRaw(string txt)
        {
            myBuffer.Append(txt);
        }

        /// <summary>
        /// 对象所包含的所有文本内容
        /// </summary>
        /// <returns>文本内容</returns>
        public override string ToString()
        {
            return myBuffer.ToString();
        }

        /// <summary>
        /// 将文本内容以指定编码格式输出到指定文件中
        /// </summary>
        /// <param name="strFileName">指定的文件名</param>
        /// <param name="encoding">指定的字符编码格式</param>
        public void WriteFile(string strFileName, System.Text.Encoding encoding)
        {
            System.IO.StreamWriter writer =
                new System.IO.StreamWriter(strFileName, false, encoding);
            writer.Write(myBuffer.ToString());
            writer.Close();
        }
    }//internal class IndentTextWriter
}